Flash Article

Migrating from Macromedia Flash MX to Flash MX 2004


Table of Contents

  1. Introduction
  2. ActionScript in Flash MX and Flash MX 2004
  3. Components in Flash MX and Flash MX 2004
  4. Video in Flash MX and Flash MX 2004
  5. Data Integration in Flash MX and Flash MX 2004
  6. Authoring Environment Changes in Flash MX 2004
  7. Screens in Flash MX Professional 2004
  8. Flash Player 7 Enhancements and Changes

ActionScript in Flash MX and Flash MX 2004

ActionScript, the scripting language for Macromedia Flash, has evolved incrementally over several releases. In the Flash MX 2004 release, ActionScript has changed enough to warrant calling it ActionScript 2.0.

ActionScript 2.0 doesn't replace ActionScript 1.0 or provide runtime features not available in ActionScript 1.0. Rather, it comprises a new set of language elements that make writing object-oriented programs much easier. With the introduction of keywords such as class, interface, extends, and implements, ActionScript 2.0 provides a formal model for writing object-oriented code—one that will be familiar to programmers who have worked with Java, JavaScript 2.0, or C#.

In addition to a new object-oriented programming model, ActionScript 2.0 provides strict data typing on variables, function parameters, and return types, as well as enhanced compiler errors and warnings that make debugging your code much easier.

ActionScript 2.0 code runs in Flash Player 6 and later. If you're targeting Flash Player 5, stick with ActionScript 1.0.

Migrating Projects to ActionScript 2.0 and Flash Player 7

You don't have to use ActionScript 2.0 to write scripts in Flash MX 2004. Any ActionScript you wrote in previous versions of Flash work in Flash MX 2004. You can continue to write code using the same techniques and language elements you're used to.

There are some situations, however, where you might want to consider migrating legacy code and projects to Flash MX 2004 and Flash Player 7:

  • You're creating a large-scale Flash application or presentation that contains a lot of code. Using ActionScript 2.0 helps speed the development and debugging process.
  • You've created object-oriented programs in ActionScript in Flash MX or Flash 5 and want to upgrade your code to ActionScript 2.0. This makes maintaining and modifying your code much easier.
  • You're interested in object-oriented programming (OOP) and want to learn about it in a familiar authoring environment.

You can combine ActionScript 1.0 and ActionScript 2.0 code in the same project, as long as you specify ActionScript 2.0 in the publish settings for your Flash document (see the next section).

Publishing to ActionScript 2.0 and ActionScript 1.0

Macromedia Flash MX 2004 has a compiler for each version of ActionScript. You specify the ActionScript version in a Flash document's Publish Setting dialog box. By default, ActionScript 2.0 is the selected ActionScript version (see Figure 1).

Publish Settings dialog box in Flash MX 2004

Figure 1. Publish Settings dialog box in Flash MX 2004

The ActionScript 2.0 compiler compiles all ActionScript 1.0 code, with the following exception: the "slash syntax" used to indicate movie clip paths (for example, parentClip/testMC:varName= "hello world") generates compilation errors if ActionScript 2.0 is selected as the ActionScript version.

To resolve this problem, either rewrite your code using dot (.) notation in place of slashes or select the ActionScript 1.0 compiler.

Creating Classes with ActionScript 2.0

The main distinction between ActionScript 2.0 and 1.0 is a formal approach to object-oriented programming. If you've ever programmed with Java, JavaScript 2.0, or C#, you can easily apply your knowledge to learning ActionScript 2.0. If you're new to OOP in general, learning ActionScript 2.0 will provide you with skills you can apply to other languages.

Some important points to remember about creating classes with ActionScript 2.0 include the following:

  • ActionScript 2.0 classes can only reside in external ActionScript (AS) files. For example, you can't define a class in a frame or button script inside a FLA file. The Script window in Flash Professional lets you easily create and edit external class files without leaving the Flash authoring environment. If you're using the standard edition of Flash MX 2004, you can use your preferred text or code editor to create AS class files.
  • The name of an AS class file must match the name of the class it contains. For example, if you create an ActionScript 2.0 class named "MyClass," then the AS file that contains that class must be named "MyClass.as."

Below is an example of a class created using ActionScript 2.0:

// Circle class using ActionScript 2.0
class Circle extends Shape {
   var radius:Number;

   function Circle(radius:Number) {
      this.radius = radius;
   }
   
   function setRadius(radius:Number):Void {
      this.radius = radius;
   }
}

And below is the same Circle class defined using ActionScript 1.0:

// Circle class using ActionScript 1.0
function Circle (radius) {
   this.radius = radius;
}

Circle.prototype = new Shape();

Circle.prototype.setRadius = function (radius) {
   this.radius = radius;
}

Although this is a simple example, it's clear that ActionScript 2.0 provides a more intuitive programming model than ActionScript 1.0 for creating classes. Note the use of strict data typing in the ActionScript 2.0 example to indicate variable types, as well as function parameter and function return value types. For more information on strict data typing, see the next section.

For a complete discussion of creating classes with ActionScript 2.0, see Creating Classes with ActionScript 2.0 in the ActionScript Reference Guide.

Strict Data Typing in ActionScript 2.0

One feature that distinguishes ActionScript 2.0 from ActionScript 1.0 is strict data typing, which is the ability to specify the kind of data that can be assigned to a variable, passed to a function as a parameter, or returned by a function. Data typing enables the ActionScript 2.0 compiler to provide more thorough debugging information than was possible using ActionScript 1.0. You can use strict data typing in your FLA scripts and external class files.

To specify a data type for a variable, function parameter, or return type, you use colon (:) syntax. For example, the following code indicates that the variable xPosition is a Number data type:

var xPosition:Number;

If at some other point in the script you assign a value to xPosition that is not a Number data type, the ActionScript 2.0 compiler generates an error.

In addition to typing variables, you can also type function parameters and return types. For example, the following function expects a parameter of type String and returns a Number:

function stringToNum(numString:String):Number {
   // 
}

Note that strict data typing is a compile-time only feature. Flash Player does not enforce strict data typing at runtime.

Case-Sensitivity in ActionScript 2.0 and Flash Player 7

Macromedia implemented several changes in Flash Player 7 to conform more closely to the ECMA-262 Edition 4 Proposal. For a full list of these changes in Flash Player 7, see ECMA-262 Edition 4 Compliance in the ActionScript Reference Guide.

Most significantly, Flash Player 7 is now case-sensitive when interpreting variable and function names. This means, for example, that two variables whose names are different only in case (foo and Foo) are interpreted as being different variables in Flash Player 7. In Flash Player 6 and earlier, those variables are interpreted as being the same.

Like all versions of Flash Player, Flash Player 7 is backward-compatible: it plays SWFs that were published to earlier SWF formats. Case-sensitivity is enforced by Flash Player 7 only on SWFs published to the Flash Player 7 format. If you publish a SWF from Flash MX 2004 to Flash Player 6 format or earlier, Flash Player 7 doesn't enforce case-sensitivity on that SWF.

If you're careful about using consistent capitalization in your code, then this change shouldn't affect your scripts. However, some scripts that ran as expected in Flash Player 6 may not run the same in Flash Player 7 when published to the Flash Player 7 format.

Case-Sensitivity at Runtime and Compile-Time

ActionScript 2.0 enforces case-sensitivity at compile time. Depending on which version of ActionScript you are using, and what version of Flash Player you are publishing to, you may get different results at compile-time and runtime. Table 1 summarizes those differences.

Table 1. Case-Sensitivity Differences at Runtime and Compile-Time
Scenario Case-Sensitivity Rules
Flash Player 7
ActionScript 1.0 or 2.0
When you publish files to Flash Player 7 or later, Flash implements case-sensitivity and usage of reserved words at compile-time and at runtime, whether you use ActionScript 1.0 or ActionScript 2.0. This means that keywords, class names, variables, method names, and so on are all case-sensitive. Variable names that differ only in capitalization are interpreted as being different variables. This change affects files loaded with #include and external variables loaded with LoadVars.load().
Flash Player 6
ActionScript 2.0
When you specify ActionScript 2.0 and publish to Flash Player 6, case-sensitivity for variables used within classes (and reserved words) is checked at compile-time, even though runtime is case-insensitive. This means you could have two class member variables, foo and Foo, which the compiler allows you to define but which point to the same variable at runtime. Because of this, any functionality that depends on foo and Foo existing as separate variables could fail silently.
Flash Player 6 or earlier
ActionScript 1.0
If you use ActionScript 1.0 and publish to Flash Player 6 or earlier, Flash works the same way as in Flash MX. Case-sensitivity for variables is not implemented. Variable names that differ only in case are considered the same and variable names cannot be the same as keywords even if they differ in case from the keyword. This same behavior exists in Flash MX today.

Observing ActionScript 2.0 Reserved Words

ActionScript 2.0 introduces several new keywords that have special meaning to the ActionScript 2.0 compiler, including class, interface, extends, and several others. If you have a script that uses any of these reserved words, and are publishing to ActionScript 2.0, the compiler generates error messages. For a full list of ActionScript 2.0 keywords, see New Object-Oriented Programming Model in the ActionScript Reference Guide.